package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.warning.model.SysWarningTips;
import com.easysoft.framework.warning.model.SysWarningTipsQuery;
import com.easysoft.framework.warning.model.SysWarningTipsView;
import com.easysoft.framework.warning.service.SysWarningTipsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = SystemWarningAction.BASE_VIEW)
public class SystemWarningAction {
    public static final String BASE_VIEW = "/warning";
    private static final Logger logger = LoggerFactory.getLogger(SystemWarningAction.class);

    @Autowired
    @Qualifier("service.SysWarningTipsService")
    private SysWarningTipsService warningTipsService;

    @RequestMapping(value = "/listWarning",method = RequestMethod.GET)
    public ModelAndView listWarning(Page page, SysWarningTipsQuery query, HttpSession session){
        logger.info("Call SystemWarningAction.listWarning({},{})", page.toString(), query.toString());
        List<SysWarningTipsView> listWarning = warningTipsService.selectSysWarningTipsByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listWarning");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listWarning", listWarning);
        return mv;
    }

    @RequestMapping(value = "/editWarning",method = RequestMethod.GET)
    public ModelAndView editWarning(String action, @RequestParam(required = false) String id){
        logger.info("Call SystemWarningAction.editWarning({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editWarning");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysWarningTips entity = warningTipsService.selectSysWarningTipsById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveWarning", method = RequestMethod.POST)
    public int saveWarning(String action, SysWarningTips warningTips, HttpSession session) {
        logger.info("Call SystemWarningAction.saveWarning({},{})", action, warningTips.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(warningTips.getId())) {
                return 0;
            }
            return addSysWarningTips(warningTips, session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(warningTips.getId())) {
                return 0;
            }
            return updateSysWarningTips(warningTips, session);
        }
        return 0;
    }

    @SystemControllerLog(description = "新增预警提示")
    private int addSysWarningTips(SysWarningTips warningTips, HttpSession session) {
        logger.info("Call SystemWarningAction.addSysWarningTips({})", warningTips.toString());
        warningTips.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return warningTipsService.addSysWarningTips(warningTips);
    }

    @SystemControllerLog(description = "更新预警提示")
    private int updateSysWarningTips(SysWarningTips warningTips, HttpSession session) {
        logger.info("Call SystemWarningAction.updateSysWarningTips({})", warningTips.toString());
        warningTips.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return warningTipsService.updateSysWarningTips(warningTips);
    }

    @RequestMapping(value = "/deleteWarning",method = RequestMethod.POST)
    public int deleteSysWarningTips(String id){
        logger.info("Call SystemWarningAction.deleteSysWarningTips({})",id);
        return warningTipsService.deleteSysWarningTips(id);
    }
}
